//package com.study.cloud.config.apilog;
//
//import org.reactivestreams.Publisher;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.core.io.buffer.DataBufferFactory;
//import org.springframework.core.io.buffer.DataBufferUtils;
//import org.springframework.http.MediaType;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.Charset;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.concurrent.atomic.AtomicReference;
//
//public class AuthFilter  implements GlobalFilter, Ordered {
//    @Override
//    public int getOrder(){
//        return HIGHEST_PRECEDENCE + 1;
//    }
//
//
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpRequest request = exchange.getRequest();
//
//        return chain.filter(exchange.mutate().request(recordRequestLog(exchange)).response(recordResponseLog(exchange)).build());
//
//    }
//
//
//    /**
//     * 记录请求日志
//     */
//    private ServerHttpRequest recordRequestLog(ServerWebExchange exchange){
//        ServerHttpRequest request = exchange.getRequest();
//        MediaType mediaType = request.getHeaders().getContentType();
//        ServerHttpRequest.Builder mutate = request.mutate();
//
//        Map<String, String> requestParams = new HashMap<String, String>();
//        if(mediaType == null){
//            requestParams = request.getQueryParams().toSingleValueMap();
//        } else if(mediaType.equals(MediaType.APPLICATION_JSON) || mediaType.equals(MediaType.APPLICATION_JSON_UTF8)){
//            Object cacheBody = exchange.getAttribute("cachedRequestBodyObject");
//            String bodyStr = resolveBodyFromFlux((Flux<DataBuffer>)cacheBody);
//            // 根据结果处理自己的逻辑
//        }
//
//        return mutate.build();
//    }
//
//    /**
//     * 解析请求体
//     */
//    private static String resolveBodyFromFlux(Flux<DataBuffer> body){
//        if(body == null) return "";
//
//        AtomicReference<String> rawRef = new AtomicReference<>();
//        body.subscribe(buffer -> {
//            byte[] bytes = new byte[buffer.readableByteCount()];
//            buffer.read(bytes);
//            DataBufferUtils.release(buffer);
////            rawRefset(String.fromUTF8ByteArray(bytes));
//        });
//
//        return rawRef.get();
//    }
//
//    /**
//     * 记录响应日志
//     */
//    private ServerHttpResponseDecorator recordResponseLog(ServerWebExchange exchange){
//        ServerHttpResponse response = exchange.getResponse();
//        MediaType mediaType = exchange.getRequest().getHeaders().getContentType();
//
//        DataBufferFactory bufferFactory = response.bufferFactory();
//        ServerHttpResponseDecorator decoratorResponse = new ServerHttpResponseDecorator(response){
//            @Override
//            public Mono<Void> writeWith(Publisher<? extends DataBuffer> body){
//                if(body instanceof Flux){
//                    // 过滤上传附件请求
//                    if((mediaType != null && mediaType.equals(MediaType.MULTIPART_FORM_DATA)) || (MediaType != null && mediaType.equals(MediaType.APPLICATION_FORM_URLENCODED)){
//                        return super.writeWith(body);
//                    }
//
//                    Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body;
//                    return super.writeWith(fluxBody.map(dataBuffer -> {
//                        byte[] content =  new byte[dataBuffer.readableByteCount()];
//                        dataBuffer.read(content);
//                        String responseResult = new String(content, Charset.forName("utf-8"));
//                        // 根据结果处理自己的逻辑
//
//                        return bufferFactory.wrap(content);
//                    }));
//                }
//
//                return super.writeWith(body);
//            }
//        }
//    }
//
//}
